package com.example.Words.room.ui.Fragment;


import android.content.Context;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.NavigationUI;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethod;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;

import com.example.Words.R;
import com.example.Words.room.model.entity.Word;
import com.example.Words.room.presenter.RoomPresenterImp;
import com.example.Words.room.presenter.RoomPresenterIntf;
import com.example.Words.room.ui.view.RoomIntf;

import butterknife.BindView;
import butterknife.BindViews;
import butterknife.ButterKnife;

/**
 * A simple {@link Fragment} subclass.
 */
public class AddFragment extends Fragment implements RoomIntf {
    @BindView(R.id.englishT)
    EditText english;
    @BindView(R.id.chineseT)
    EditText chinese;
    @BindView(R.id.submit)
    Button submit;
    private RoomPresenterIntf presenterIntf;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view =inflater.inflate(R.layout.fragment_add, container, false);
        ButterKnife.bind(this,view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        submit.setEnabled(false);
        english.requestFocus();
        presenterIntf=new RoomPresenterImp(this,requireActivity().getApplication());
        InputMethodManager imm=(InputMethodManager) requireActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(english,0);
        TextWatcher textWatcher=new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                    String English=english.getText().toString().trim();
                    String Chinese=chinese.getText().toString().trim();
                    submit.setEnabled(!English.isEmpty()&&!Chinese.isEmpty());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        };
        english.addTextChangedListener(textWatcher);
        chinese.addTextChangedListener(textWatcher);
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String English=english.getText().toString().trim();
                String Chinese=chinese.getText().toString().trim();
                Word word=new Word(English,Chinese);
                presenterIntf.insertDate(word);
                NavController navController= Navigation.findNavController(v);
                navController.navigateUp();
                InputMethodManager inputMethodManager=(InputMethodManager)requireActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                if (inputMethodManager.isActive())
                inputMethodManager.hideSoftInputFromWindow(getView().getWindowToken(),0);
            }
        });
    }
    @Override
    public void changeLayout() {

    }


}
